package james.education;

import james.utils.Utils;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class FlashCardFrame extends JFrame{

	protected ImagePanel imgPanel = new ImagePanel();
	protected JPanel wordPanel = new JPanel();
	protected JPanel ctrPanel = new JPanel();
	protected JPanel optionsPanel = new JPanel();
	protected JPanel topPanel = new JPanel();
	protected JPanel leftPanel = new JPanel();
	protected JPanel bottomPanel = new JPanel();
	protected JButton prevBtn = new JButton("previous");
	protected JButton nextBtn = new JButton("next");
	
	protected List list = null;
	protected int numberOfCards = 0;
	protected int cardIndex = 0;
	protected String imageFile = null;
	protected JTextArea text = new JTextArea();
	protected JCheckBox showPictureChBx = new JCheckBox("show picture");
	protected JCheckBox markCorrectChBx = new JCheckBox("mark correct");
	
	public FlashCardFrame(String imageFile) {
		super();
		setImageFile(imageFile);
		init();		
	}
	
	private void loadImageandWord() {		
				
		CardInfo cardInfo = (CardInfo) list.get(getCardIndex());
		ImageIcon image = cardInfo.getImage();
		
		imgPanel.setImage(image.getImage());
		
		text.setText(image.getDescription());		
		
		markCorrectChBx.setSelected(cardInfo.isCorrect());
		
		showPictureChBx.setSelected(cardInfo.isShowPicture());
		imgPanel.setVisible(cardInfo.isShowPicture());
		
		if(cardInfo.isCorrect()) {
			text.setForeground(Color.GREEN);
		}
		else {
			text.setForeground(Color.BLACK);
		}
		
		imgPanel.validate();
		this.validate();
		this.repaint();

	}
	
	private void init() {	
		
		this.setTitle("Flash Cards (input file: " + this.getImageFile() +")");
		
		setSize(800,500);
		
		topPanel.add(prevBtn);
		topPanel.add(nextBtn);
		prevBtn.setBorder(BorderFactory.createRaisedBevelBorder());
		nextBtn.setBorder(BorderFactory.createRaisedBevelBorder());
		prevBtn.setEnabled(false);
		
		ButtonClickListener listener = new ButtonClickListener();
		nextBtn.addActionListener(listener);
		prevBtn.addActionListener(listener);
		
		CheckBoxListener radioListener = new CheckBoxListener();
		showPictureChBx.addItemListener(radioListener);
		markCorrectChBx.addItemListener(radioListener);
		
		wordPanel.setBackground(Color.WHITE);
//		wordPanel.setSize(200,500);
//		wordPanel.setPreferredSize(new Dimension(200,500));
//		wordPanel.setMinimumSize(new Dimension(200,500));
		text.setBackground(Color.WHITE);
		text.setFont(new Font(null, Font.BOLD, 30));
		wordPanel.add(text);
		
		list = Utils.loadImages(imageFile);
		setNumberOfCards(list.size());
		
		if ( (list ==null) || (list.size() == 0) ) {
			System.out.println("image file is empty");
			System.exit(-1);
		}				
		
		loadImageandWord();
		
		if(getNumberOfCards() > 1) {
			nextBtn.setEnabled(true);
		}
		else {
			nextBtn.setEnabled(false);
		}
		
		ctrPanel.setLayout(new GridLayout(1,2));
		ctrPanel.add(wordPanel);
		ctrPanel.add(imgPanel);
		
		getContentPane().add(ctrPanel, BorderLayout.CENTER);
		//getContentPane().add(imgPanel, BorderLayout.CENTER);
		//getContentPane().add(wordPanel,BorderLayout.WEST);
		getContentPane().add(topPanel,BorderLayout.NORTH);
		
		optionsPanel.setLayout(new GridLayout(3,1));
		optionsPanel.add(showPictureChBx);
		optionsPanel.add(markCorrectChBx);
		getContentPane().add(optionsPanel, BorderLayout.EAST);
		getContentPane().add(leftPanel, BorderLayout.WEST);
		getContentPane().add(bottomPanel, BorderLayout.SOUTH);
		
		validate();
		setVisible(true);
		
		this.addWindowListener(new FlashCardWindowListener());
	}
	
	class CheckBoxListener implements ItemListener {		
		public void itemStateChanged(ItemEvent e) {

			if(e.getSource() == showPictureChBx) {
			    JCheckBox cb = (JCheckBox) e.getSource();
			    CardInfo card = (CardInfo) list.get(cardIndex);
				card.setShowPicture(cb.isSelected());			    
			}
			else if(e.getSource() == markCorrectChBx) {
				JCheckBox cb = (JCheckBox) e.getSource();
				CardInfo card = (CardInfo) list.get(cardIndex);
				card.setCorrect(cb.isSelected());
			}
			
			loadImageandWord();
			
		}
	}
	
	class FlashCardWindowListener extends WindowAdapter {
		
		public void windowClosing(WindowEvent we) {
        	System.out.println("closing flash card program");
        	System.exit(-1);
        }
		
	}
	
	class ButtonClickListener implements ActionListener {		
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == prevBtn) {
				setCardIndex(getCardIndex() - 1);				
			}
			else if(e.getSource() == nextBtn) {
				setCardIndex(getCardIndex() + 1);				
			}
			
			if(getCardIndex() == 0) {
                prevBtn.setEnabled(false);					
			}
			else {
				prevBtn.setEnabled(true);
			}
			if(getCardIndex() == (getNumberOfCards() - 1)) {
                nextBtn.setEnabled(false);					
			}
			else {
				nextBtn.setEnabled(true);
			}
			
			loadImageandWord();
			
		}
	}


	public int getCardIndex() {
		return cardIndex;
	}

	public void setCardIndex(int cardIndex) {
		this.cardIndex = cardIndex;
	}

	public JPanel getCtrPanel() {
		return ctrPanel;
	}

	public void setCtrPanel(JPanel ctrPanel) {
		this.ctrPanel = ctrPanel;
	}

	public ImagePanel getImgPanel() {
		return imgPanel;
	}

	public void setImgPanel(ImagePanel imgPanel) {
		this.imgPanel = imgPanel;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public JButton getNextBtn() {
		return nextBtn;
	}

	public void setNextBtn(JButton nextBtn) {
		this.nextBtn = nextBtn;
	}

	public int getNumberOfCards() {
		return numberOfCards;
	}

	public void setNumberOfCards(int numberOfCards) {
		this.numberOfCards = numberOfCards;
	}

	public JButton getPrevBtn() {
		return prevBtn;
	}

	public void setPrevBtn(JButton prevBtn) {
		this.prevBtn = prevBtn;
	}

	public JPanel getTopPanel() {
		return topPanel;
	}

	public void setTopPanel(JPanel topPanel) {
		this.topPanel = topPanel;
	}

	public JPanel getWordPanel() {
		return wordPanel;
	}

	public void setWordPanel(JPanel wordPanel) {
		this.wordPanel = wordPanel;
	}

	public String getImageFile() {
		return imageFile;
	}

	public void setImageFile(String imageFile) {
		this.imageFile = imageFile;
	}

	public JTextArea getLabel() {
		return text;
	}

	public void setLabel(JTextArea label) {
		this.text = label;
	}
	
	public static void main(String args []) {
		JFrame FlashCardFrame = new FlashCardFrame("c:/temp/flashCards.txt");
	}

	public JCheckBox getMarkCorrectChBx() {
		return markCorrectChBx;
	}

	public void setMarkCorrectChBx(JCheckBox markCorrectChBx) {
		this.markCorrectChBx = markCorrectChBx;
	}

	public JCheckBox getShowPictureChBx() {
		return showPictureChBx;
	}

	public void setShowPictureChBx(JCheckBox showPictureChBx) {
		this.showPictureChBx = showPictureChBx;
	}

	public JPanel getOptionsPanel() {
		return optionsPanel;
	}

	public void setOptionsPanel(JPanel optionsPanel) {
		this.optionsPanel = optionsPanel;
	}

	public JPanel getBottomPanel() {
		return bottomPanel;
	}

	public void setBottomPanel(JPanel bottomPanel) {
		this.bottomPanel = bottomPanel;
	}

	public JPanel getLeftPanel() {
		return leftPanel;
	}

	public void setLeftPanel(JPanel leftPanel) {
		this.leftPanel = leftPanel;
	}

	public JTextArea getText() {
		return text;
	}

	public void setText(JTextArea text) {
		this.text = text;
	}
	
}
